package com.qiantu.phone.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;


/**
 * Created by rtyui on 2018/4/28.
 * <p>
 * 效果：ScrollView头部底部弹性
 * <p>
 * tip0:弹性尺寸为 move/2
 * <p>
 * tip1:可以双指同时拉动 这时候执行的事件 只有 一次down 一次up
 * <p>
 * tip2:可以在动画回弹时继续拉动
 * <p>
 * tip3:可以在滑动屏幕的过程中不用抬起直接拉动
 */


public class MoveView extends LinearLayout {

    private float move;//移动距离
    private float mPrevX;//记录开始位置，处理viewpager
    private float mPrevY = 0;
    private int mTouchSlop;
    private int mActivePointerId;
    private float mPrimaryLastY;
    private float mPrimaryLastX;
    private float mInitialDownY;
    private VelocityTracker mVelocityTracker;

    public MoveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    //处理touch事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
        int index = -1;
        try {
            obtainVelocity(event);
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    index = event.getActionIndex();
                    mActivePointerId = event.getPointerId(index);
                    Log.i("luzx", "onTouchEvent ACTION_DOWN:" + mActivePointerId);
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    mPrimaryLastY = 0;
                    index = event.getActionIndex();
                    mActivePointerId = event.getPointerId(index);
                    Log.i("luzx", "onTouchEvent ACTION_POINTER_DOWN:" + mActivePointerId);
                    break;
                case MotionEvent.ACTION_MOVE:
                    index = event.findPointerIndex(mActivePointerId);
                    if (index == -1) {
                        index = event.getActionIndex();
                        mActivePointerId = event.getPointerId(index);
                        mPrimaryLastY = event.getY(index);
                        return true;
                    }
                        if (mPrimaryLastY == 0) {
                            mInitialDownY = event.getY(index);
                            mPrimaryLastY = event.getY(index);
                            mPrimaryLastX = event.getX(index);
                        } else {
                            float x = event.getX(index);
                            float y = event.getY(index);
                            //横坐标位移增量
                            float deltaX = Math.abs(x - mPrimaryLastX);
                            //纵坐标位移增量
                            float deltaY = Math.abs(y - mPrimaryLastY);
                            mVelocityTracker.computeCurrentVelocity(1000);
                            float xVelocity = mVelocityTracker.getXVelocity();
                            float yVelocity = mVelocityTracker.getYVelocity();
                            // 1.如果x方向速度大于y方向速度，且大于最小速度限制；
                            // 2.如果x方向的侧滑距离大于y方向滑动距离；
                            mPrimaryLastX = x;
                            Log.i("luzx", "currentPosition:" + y + "--lastPosition:" + mPrimaryLastY);
                            Log.i("luzx", "currentPosition:" + y + "--mInitialDownY:" + mInitialDownY);
                            Log.i("luzx", "mTouchSlop:" + mTouchSlop);
                            float temp = y - mPrimaryLastY;
                            mPrimaryLastY = y;
//                            Log.i("luzx", "deltaX:" + deltaX + "--deltaY:" + deltaY);
//                            if(Math.abs(xVelocity) > SNAP_VELOCITY && Math.abs(xVelocity) > Math.abs(yVelocity) || deltaX >= deltaY){
//                                mInitialDownY = y;
//                                Log.i("luzx","mInitialDownY:" + mInitialDownY);
//                                return false;
//                            }
                            if (Math.abs(y - mInitialDownY) <= mTouchSlop) {
                                return true;
                            }
                            move += temp;
                            if(move > 0){
                                move = 0;
                                return true;
                            }
                        }
//                        move = 0;
                    //正常滑动记录
                    mPrimaryLastY = event.getY(index);
                    Log.i("luzx", "getScrollY():" + getScrollY());
//                }
                    break;
                case MotionEvent.ACTION_POINTER_UP:
//                    mPrimaryLastY = 0;
                    break;
                case MotionEvent.ACTION_UP:

                    mPrimaryLastY = 0;
                    releaseVelocity();
                    break;
                case MotionEvent.ACTION_CANCEL:

                    mPrimaryLastY = 0;
                    releaseVelocity();
                    break;
            }
            return super.onTouchEvent(event);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return false;
        }
    }

    private void releaseVelocity() {
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
    }

    private void obtainVelocity(MotionEvent event) {
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        int index;
        try {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    index = event.getActionIndex();
                    mPrevY = event.getY(index);
                    mPrevX = event.getX(index);
                    mActivePointerId = event.getPointerId(index);
                    Log.i("luzx", "onInterceptTouchEvent ACTION_DOWN:" + mActivePointerId);
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    index = event.getActionIndex();
                    mPrevY = event.getY(index);
                    mPrevX = event.getX(index);
                    mActivePointerId = event.getPointerId(index);
                    Log.i("luzx", "onInterceptTouchEvent ACTION_POINTER_DOWN:" + mActivePointerId);
                    break;
                case MotionEvent.ACTION_MOVE:
                    index = event.findPointerIndex(mActivePointerId);
            }
            Log.i("luzx", "super.onInterceptTouchEvent(com.qiantu.phone.event):" + super.onInterceptTouchEvent(event));
            return super.onInterceptTouchEvent(event);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return false;
        }
    }
}
